package com.uuah.server.exception.service;

import com.uuah.config.i18n.Message;
import com.uuah.server.api.IUuahMessage;
import com.uuah.server.api.component.service.IService;
import com.uuah.server.config.i18n.ServerMessages;
import com.uuah.server.exception.MessagingException;

/**
 * <code>ServiceException</code> should be thrown when some action on a service
 * fails, such as starting or stopping
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午01:42:20
 * @version 1.0.0
 */
public class ServiceException extends MessagingException {
	private static final long serialVersionUID = 56178344205041599L;

	private final transient IService service;

	public ServiceException(Message message, IUuahMessage umoMessage,
			IService service) {
		super(generateMessage(message, service), umoMessage);
		this.service = service;
	}

	public ServiceException(Message message, IUuahMessage umoMessage,
			IService service, Throwable cause) {
		super(generateMessage(message, service), umoMessage, cause);
		this.service = service;
	}

	public ServiceException(IUuahMessage umoMessage, IService service,
			Throwable cause) {
		super(generateMessage(null, service), umoMessage, cause);
		this.service = service;
	}

	public IService getService() {
		return service;
	}

	private static Message generateMessage(Message previousMessage,
			IService service) {
		Message returnMessage = ServerMessages.componentCausedErrorIs(service);
		if (previousMessage != null) {
			previousMessage.setNextMessage(returnMessage);
			return previousMessage;
		} else {
			return returnMessage;
		}
	}
}
